<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .content {
            width: 300px;
            height: 300px;
            margin: 0 auto;
            text-align: center;
            font-size: 30px;
            background-color: whitesmoke;
        }
    </style>
</head>

<body>
    <div class="content">0</div>
    <script src="./js/lodash.min.js"></script>
    <script>
        // 1、使用lodash防抖函数
        // const box=document.querySelector('.content')
        // let i=0
        // box.addEventListener('mousemove',_.debounce(addI,500))
        // function addI(){
        //     i++
        //     box.innerHTML=i
        // }

        // 2、定时器
        const box = document.querySelector('.content')
        let i = 0
        box.addEventListener('mousemove', debounce(addI,500))
        function addI() {
            i++
            box.innerHTML = i
        }
        function debounce(fn,t){
            let timerId = 0
            return function(){
                if(timerId!==0) clearTimeout(timerId)
                timerId=setTimeout(function(){
                    fn()
                },t)
            }
        }
    </script>

</body>

</html>